home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / imainarg.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  23.5 KB  |  908 lines

  1. /* Copyright (C) 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: imainarg.c,v 1.4.2.2 2000/11/09 22:45:17 rayjj Exp $ */
  20. /* Command line parsing and dispatching */
  21. #include "ctype_.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "gp.h"
  26. #include "gsargs.h"
  27. #include "gscdefs.h"
  28. #include "gsmalloc.h"        /* for gs_malloc_limit */
  29. #include "gsmdebug.h"
  30. #include "gxdevice.h"
  31. #include "gxdevmem.h"
  32. #include "gsdevice.h"
  33. #include "stream.h"
  34. #include "errors.h"
  35. #include "estack.h"
  36. #include "ialloc.h"
  37. #include "strimpl.h"        /* for sfilter.h */
  38. #include "sfilter.h"        /* for iscan.h */
  39. #include "ostack.h"        /* must precede iscan.h */
  40. #include "iscan.h"
  41. #include "iconf.h"
  42. #include "imain.h"
  43. #include "imainarg.h"
  44. #include "iminst.h"
  45. #include "iname.h"
  46. #include "store.h"
  47. #include "files.h"        /* requires stream.h */
  48. #include "interp.h"
  49. #include "iutil.h"
  50. #include "ivmspace.h"
  51.  
  52. /* Import operator procedures */
  53. extern int zflush(P1(i_ctx_t *));
  54. extern int zflushpage(P1(i_ctx_t *));
  55.  
  56. #ifndef GS_LIB
  57. #  define GS_LIB "GS_LIB"
  58. #endif
  59.  
  60. #ifndef GS_OPTIONS
  61. #  define GS_OPTIONS "GS_OPTIONS"
  62. #endif
  63.  
  64. #ifndef GS_MAX_LIB_DIRS
  65. #  define GS_MAX_LIB_DIRS 25
  66. #endif
  67.  
  68. #ifndef GS_BUG_MAILBOX
  69. #  define GS_BUG_MAILBOX "bug-gs@aladdin.com"
  70. #endif
  71.  
  72. #define MAX_BUFFERED_SIZE 1024
  73.  
  74. /* Note: sscanf incorrectly defines its first argument as char * */
  75. /* rather than const char *.  This accounts for the ugly casts below. */
  76.  
  77. /* Redefine puts to use fprintf, so it will work even without stdio. */
  78. #undef puts
  79. private void
  80. fpputs(const gs_main_instance *minst, const char *str)
  81. {
  82.     fprintf(minst->fstdout, "%s\n", str);
  83. }
  84. #define puts(str) fpputs(minst, str)
  85.  
  86. /* Forward references */
  87. #define runInit 1
  88. #define runFlush 2
  89. #define runBuffer 4
  90. private int swproc(P3(gs_main_instance *, const char *, arg_list *));
  91. private int argproc(P2(gs_main_instance *, const char *));
  92. private int run_buffered(P2(gs_main_instance *, const char *));
  93. private int esc_strlen(P1(const char *));
  94. private void esc_strcat(P2(char *, const char *));
  95. private int runarg(P5(gs_main_instance *, const char *, const char *, const char *, int));
  96. private int run_string(P3(gs_main_instance *, const char *, int));
  97. private int run_finish(P4(gs_main_instance *, int, int, ref *));
  98.  
  99. /* Forward references for help printout */
  100. private void print_help(P1(gs_main_instance *));
  101. private void print_revision(P1(const gs_main_instance *));
  102. private void print_version(P1(const gs_main_instance *));
  103. private void print_usage(P1(const gs_main_instance *));
  104. private void print_devices(P1(const gs_main_instance *));
  105. private void print_emulators(P1(const gs_main_instance *));
  106. private void print_paths(P1(gs_main_instance *));
  107. private void print_help_trailer(P1(const gs_main_instance *));
  108.  
  109. /* ------ Main program ------ */
  110.  
  111. /* Process the command line with a given instance. */
  112. private FILE *
  113. gs_main_arg_fopen(const char *fname, void *vminst)
  114. {
  115.     gs_main_set_lib_paths((gs_main_instance *) vminst);
  116.     return lib_fopen(fname);
  117. }
  118. #define arg_heap_copy(str) arg_copy(str, &gs_memory_default)
  119. int
  120. gs_main_init_with_args(gs_main_instance * minst, int argc, char *argv[])
  121. {
  122.     const char *arg;
  123.     arg_list args;
  124.     FILE *stdfiles[3];
  125.     int code;
  126.  
  127.     gs_get_real_stdio(stdfiles);
  128.     arg_init(&args, (const char **)argv, argc,
  129.          gs_main_arg_fopen, (void *)minst);
  130.     code = gs_main_init0(minst, stdfiles[0], stdfiles[1], stdfiles[2],
  131.              GS_MAX_LIB_DIRS);
  132.     if (code < 0)
  133.     return code;
  134.     {
  135.     int len = 0;
  136.     int code = gp_getenv(GS_LIB, (char *)0, &len);
  137.  
  138.     if (code < 0) {        /* key present, value doesn't fit */
  139.         char *path = (char *)gs_alloc_bytes(minst->heap, len, "GS_LIB");
  140.  
  141.         gp_getenv(GS_LIB, path, &len);    /* can't fail */
  142.         minst->lib_path.env = path;
  143.     }
  144.     }
  145.     minst->lib_path.final = gs_lib_default_path;
  146.     code = gs_main_set_lib_paths(minst);
  147.     if (code < 0)
  148.     return code;
  149.     /* Prescan the command line for --help and --version. */
  150.     {
  151.     int i;
  152.     bool helping = false;
  153.  
  154.     for (i = 1; i < argc; ++i)
  155.         if (!strcmp(argv[i], "--")) {
  156.         /* A PostScript program will be interpreting all the */
  157.         /* remaining switches, so stop scanning. */
  158.         helping = false;
  159.         break;
  160.         } else if (!strcmp(argv[i], "--help")) {
  161.         print_help(minst);
  162.         helping = true;
  163.         } else if (!strcmp(argv[i], "--version")) {
  164.         print_version(minst);
  165.         puts("");    /* \n */
  166.         helping = true;
  167.         }
  168.     if (helping)
  169.         gs_exit(gs_exit_INFO);
  170.     }
  171.     /* Execute files named in the command line, */
  172.     /* processing options along the way. */
  173.     /* Wait until the first file name (or the end */
  174.     /* of the line) to finish initialization. */
  175.     minst->run_start = true;
  176.  
  177.     {
  178.     int len = 0;
  179.     int code = gp_getenv(GS_OPTIONS, (char *)0, &len);
  180.  
  181.     if (code < 0) {        /* key present, value doesn't fit */
  182.         char *opts =
  183.         (char *)gs_alloc_bytes(minst->heap, len, "GS_OPTIONS");
  184.  
  185.         gp_getenv(GS_OPTIONS, opts, &len);    /* can't fail */
  186.         arg_push_memory_string(&args, opts, minst->heap);
  187.     }
  188.     }
  189.     while ((arg = arg_next(&args)) != 0) {
  190.     switch (*arg) {
  191.         case '-':
  192.         code = swproc(minst, arg, &args);
  193.         if (code < 0)
  194.             return code;
  195.         if (code > 0)
  196.             fprintf(minst->fstdout,
  197.                 "Unknown switch %s - ignoring\n", arg);
  198.         break;
  199.         default:
  200.         code = argproc(minst, arg);
  201.         if (code < 0)
  202.             return code;
  203.     }
  204.     }
  205.  
  206.     return gs_main_init2(minst);
  207. }
  208.  
  209. /*
  210.  * Run the 'start' procedure (after processing the command line).
  211.  * Note that in case of error, this procedure exits rather than returning.
  212.  */
  213. void
  214. gs_main_run_start(gs_main_instance * minst)
  215. {
  216.     run_string(minst, "systemdict /start get exec", runFlush);
  217. }
  218.  
  219. /* Process switches.  Return 0 if processed, 1 for unknown switch, */
  220. /* <0 if error. */
  221. private int
  222. swproc(gs_main_instance * minst, const char *arg, arg_list * pal)
  223. {
  224.     char sw = arg[1];
  225.     ref vtrue;
  226.     int code;
  227. #undef initial_enter_name
  228. #define initial_enter_name(nstr, pvalue)\
  229.   i_initial_enter_name(minst->i_ctx_p, nstr, pvalue)
  230.  
  231.     make_true(&vtrue);
  232.     arg += 2;            /* skip - and letter */
  233.     switch (sw) {
  234.     default:
  235.         return 1;
  236.     case 0:        /* read stdin as a file char-by-char */
  237.         /* This is a ******HACK****** for Ghostview. */
  238.         minst->stdin_is_interactive = true;
  239.         goto run_stdin;
  240.     case '_':    /* read stdin with normal buffering */
  241.         minst->stdin_is_interactive = false;
  242. run_stdin:
  243.         minst->run_start = false;    /* don't run 'start' */
  244.         /* Set NOPAUSE so showpage won't try to read from stdin. */
  245.         code = swproc(minst, "-dNOPAUSE", pal);
  246.         if (code)
  247.         return code;
  248.         code = gs_main_init2(minst);    /* Finish initialization */
  249.         if (code < 0)
  250.         return code;
  251.         gs_stdin_is_interactive = minst->stdin_is_interactive;
  252.         run_string(minst, ".runstdin", runFlush);
  253.         break;
  254.     case '-':        /* run with command line args */
  255.     case '+':
  256.         pal->expand_ats = false;
  257.     case '@':        /* ditto with @-expansion */
  258.         {
  259.         const char *psarg = arg_next(pal);
  260.  
  261.         if (psarg == 0) {
  262.             fprintf(minst->fstdout,
  263.                 "Usage: gs ... -%c file.ps arg1 ... argn\n", sw);
  264.             arg_finit(pal);
  265.             gs_exit(1);
  266.         }
  267.         psarg = arg_heap_copy(psarg);
  268.         code = gs_main_init2(minst);
  269.         if (code < 0)
  270.             return code;
  271.         run_string(minst, "userdict/ARGUMENTS[", 0);
  272.         while ((arg = arg_next(pal)) != 0)
  273.             runarg(minst, "", arg_heap_copy(arg), "", runInit);
  274.         runarg(minst, "]put", psarg, ".runfile", runInit | runFlush);
  275.         gs_exit(0);
  276.         }
  277.     case 'A':        /* trace allocator */
  278.         switch (*arg) {
  279.         case 0:
  280.             gs_alloc_debug = 1;
  281.             break;
  282.         case '-':
  283.             gs_alloc_debug = 0;
  284.             break;
  285.         default:
  286.             puts("-A may only be followed by -");
  287.             gs_exit(1);
  288.         }
  289.         break;
  290.     case 'B':        /* set run_string buffer size */
  291.         if (*arg == '-')
  292.         minst->run_buffer_size = 0;
  293.         else {
  294.         uint bsize;
  295.  
  296.         if (sscanf((const char *)arg, "%u", &bsize) != 1 ||
  297.             bsize <= 0 || bsize > MAX_BUFFERED_SIZE
  298.             ) {
  299.             fprintf(minst->fstdout, "-B must be followed by - or size between 1 and %u\n", MAX_BUFFERED_SIZE);
  300.             gs_exit(1);
  301.         }
  302.         minst->run_buffer_size = bsize;
  303.         }
  304.         break;
  305.     case 'c':        /* code follows */
  306.         {
  307.         bool ats = pal->expand_ats;
  308.  
  309.         code = gs_main_init2(minst);
  310.         if (code < 0)
  311.             return code;
  312.         pal->expand_ats = false;
  313.         while ((arg = arg_next(pal)) != 0) {
  314.             char *sarg;
  315.  
  316.             if (arg[0] == '@' ||
  317.             (arg[0] == '-' && !isdigit(arg[1]))
  318.             )
  319.             break;
  320.             sarg = arg_heap_copy(arg);
  321.             runarg(minst, "", sarg, ".runstring", 0);
  322.         }
  323.         if (arg != 0)
  324.             arg_push_string(pal, arg_heap_copy(arg));
  325.         pal->expand_ats = ats;
  326.         break;
  327.         }
  328.     case 'E':        /* log errors */
  329.         switch (*arg) {
  330.         case 0:
  331.             gs_log_errors = 1;
  332.             break;
  333.         case '-':
  334.             gs_log_errors = 0;
  335.             break;
  336.         default:
  337.             puts("-E may only be followed by -");
  338.             gs_exit(1);
  339.         }
  340.         break;
  341.     case 'f':        /* run file of arbitrary name */
  342.         if (*arg != 0)
  343.         argproc(minst, arg);
  344.         break;
  345.     case 'F':        /* run file with buffer_size = 1 */
  346.         if (!*arg) {
  347.         puts("-F requires a file name");
  348.         gs_exit(1);
  349.         } {
  350.         uint bsize = minst->run_buffer_size;
  351.  
  352.         minst->run_buffer_size = 1;
  353.         argproc(minst, arg);
  354.         minst->run_buffer_size = bsize;
  355.         }
  356.         break;
  357.     case 'g':        /* define device geometry */
  358.         {
  359.         long width, height;
  360.         ref value;
  361.  
  362.         gs_main_init1(minst);
  363.         if (sscanf((const char *)arg, "%ldx%ld", &width, &height) != 2) {
  364.             puts("-g must be followed by <width>x<height>");
  365.             gs_exit(1);
  366.         }
  367.         make_int(&value, width);
  368.         initial_enter_name("DEVICEWIDTH", &value);
  369.         make_int(&value, height);
  370.         initial_enter_name("DEVICEHEIGHT", &value);
  371.         initial_enter_name("FIXEDMEDIA", &vtrue);
  372.         break;
  373.         }
  374.     case 'h':        /* print help */
  375.     case '?':        /* ditto */
  376.         print_help(minst);
  377.         gs_exit(gs_exit_INFO);
  378.     case 'I':        /* specify search path */
  379.         gs_main_add_lib_path(minst, arg_heap_copy(arg));
  380.         break;
  381.     case 'K':        /* set malloc limit */
  382.         {
  383.         long msize = 0;
  384.  
  385.         sscanf((const char *)arg, "%ld", &msize);
  386.         if (msize <= 0 || msize > max_long >> 10) {
  387.             fprintf(minst->fstdout,
  388.                 "-K<numK> must have 1 <= numK <= %ld\n",
  389.                 max_long >> 10);
  390.             gs_exit(1);
  391.         }
  392.         gs_malloc_limit = msize << 10;
  393.         }
  394.         break;
  395.     case 'M':        /* set memory allocation increment */
  396.         {
  397.         unsigned msize = 0;
  398.  
  399.         sscanf((const char *)arg, "%u", &msize);
  400. #if arch_ints_are_short
  401.         if (msize <= 0 || msize >= 64) {
  402.             puts("-M must be between 1 and 63");
  403.             gs_exit(1);
  404.         }
  405. #endif
  406.         minst->memory_chunk_size = msize << 10;
  407.         }
  408.         break;
  409.     case 'N':        /* set size of name table */
  410.         {
  411.         unsigned nsize = 0;
  412.  
  413.         sscanf((const char *)arg, "%d", &nsize);
  414. #if arch_ints_are_short
  415.         if (nsize < 2 || nsize > 64) {
  416.             puts("-N must be between 2 and 64");
  417.             gs_exit(1);
  418.         }
  419. #endif
  420.         minst->name_table_size = (ulong) nsize << 10;
  421.         }
  422.         break;
  423.     case 'P':        /* choose whether search '.' first */
  424.         if (!strcmp(arg, ""))
  425.         minst->search_here_first = true;
  426.         else if (!strcmp(arg, "-"))
  427.         minst->search_here_first = false;
  428.         else {
  429.         puts("Only -P or -P- is allowed.");
  430.         gs_exit(1);
  431.         }
  432.         break;
  433.     case 'q':        /* quiet startup */
  434.         gs_main_init1(minst);
  435.         initial_enter_name("QUIET", &vtrue);
  436.         break;
  437.     case 'r':        /* define device resolution */
  438.         {
  439.         float xres, yres;
  440.         ref value;
  441.  
  442.         gs_main_init1(minst);
  443.         switch (sscanf((const char *)arg, "%fx%f", &xres, &yres)) {
  444.             default:
  445.             puts("-r must be followed by <res> or <xres>x<yres>");
  446.             gs_exit(1);
  447.             case 1:    /* -r<res> */
  448.             yres = xres;
  449.             case 2:    /* -r<xres>x<yres> */
  450.             make_real(&value, xres);
  451.             initial_enter_name("DEVICEXRESOLUTION", &value);
  452.             make_real(&value, yres);
  453.             initial_enter_name("DEVICEYRESOLUTION", &value);
  454.             initial_enter_name("FIXEDRESOLUTION", &vtrue);
  455.         }
  456.         break;
  457.         }
  458.     case 'D':        /* define name */
  459.     case 'd':
  460.     case 'S':        /* define name as string */
  461.     case 's':
  462.         {
  463.         char *adef = arg_heap_copy(arg);
  464.         char *eqp = strchr(adef, '=');
  465.         bool isd = (sw == 'D' || sw == 'd');
  466.         ref value;
  467.  
  468.         if (eqp == NULL)
  469.             eqp = strchr(adef, '#');
  470.         /* Initialize the object memory, scanner, and */
  471.         /* name table now if needed. */
  472.         gs_main_init1(minst);
  473.         if (eqp == adef) {
  474.             puts("Usage: -dname, -dname=token, -sname=string");
  475.             gs_exit(1);
  476.         }
  477.         if (eqp == NULL) {
  478.             if (isd)
  479.             make_true(&value);
  480.             else
  481.             make_empty_string(&value, a_readonly);
  482.         } else {
  483.             int code;
  484.             i_ctx_t *i_ctx_p = minst->i_ctx_p;
  485.             uint space = icurrent_space;
  486.  
  487.             *eqp++ = 0;
  488.             ialloc_set_space(idmemory, avm_system);
  489.             if (isd) {
  490.             stream astream;
  491.             scanner_state state;
  492.  
  493.             sread_string(&astream,
  494.                      (const byte *)eqp, strlen(eqp));
  495.             scanner_state_init(&state, false);
  496.             code = scan_token(minst->i_ctx_p, &astream, &value,
  497.                       &state);
  498.             if (code) {
  499.                 puts("-dname= must be followed by a valid token");
  500.                 gs_exit(1);
  501.             }
  502.             if (r_has_type_attrs(&value, t_name,
  503.                          a_executable)) {
  504.                 ref nsref;
  505.  
  506.                 name_string_ref(&value, &nsref);
  507. #define string_is(nsref, str, len)\
  508.   (r_size(&(nsref)) == (len) &&\
  509.    !strncmp((const char *)(nsref).value.const_bytes, str, (len)))
  510.                 if (string_is(nsref, "null", 4))
  511.                 make_null(&value);
  512.                 else if (string_is(nsref, "true", 4))
  513.                 make_true(&value);
  514.                 else if (string_is(nsref, "false", 5))
  515.                 make_false(&value);
  516.                 else {
  517.                 puts("-dvar=name requires name=null, true, or false");
  518.                 gs_exit(1);
  519.                 }
  520. #undef name_is_string
  521.             }
  522.             } else {
  523.             int len = strlen(eqp);
  524.             char *str =
  525.             (char *)gs_alloc_bytes(minst->heap,
  526.                            (uint) len, "-s");
  527.  
  528.             if (str == 0) {
  529.                 lprintf("Out of memory!\n");
  530.                 gs_exit(1);
  531.             }
  532.             memcpy(str, eqp, len);
  533.             make_const_string(&value,
  534.                       a_readonly | avm_foreign,
  535.                       len, (const byte *)str);
  536.             }
  537.             ialloc_set_space(idmemory, space);
  538.         }
  539.         /* Enter the name in systemdict. */
  540.         initial_enter_name(adef, &value);
  541.         break;
  542.         }
  543.     case 'u':        /* undefine name */
  544.         if (!*arg) {
  545.         puts("-u requires a name to undefine.");
  546.         gs_exit(1);
  547.         }
  548.         gs_main_init1(minst);
  549.         i_initial_remove_name(minst->i_ctx_p, arg);
  550.         break;
  551.     case 'v':        /* print revision */
  552.         print_revision(minst);
  553.         gs_exit(0);
  554. /*#ifdef DEBUG */
  555.         /*
  556.          * Here we provide a place for inserting debugging code that can be
  557.          * run in place of the normal interpreter code.
  558.          */
  559.     case 'X':
  560.         code = gs_main_init2(minst);
  561.         if (code < 0)
  562.         return code;
  563.         {
  564.         int xec;    /* exit_code */
  565.         ref xeo;    /* error_object */
  566.  
  567. #define start_x()\
  568.   gs_main_run_string_begin(minst, 1, &xec, &xeo)
  569. #define run_x(str)\
  570.   gs_main_run_string_continue(minst, str, strlen(str), 1, &xec, &xeo)
  571. #define stop_x()\
  572.   gs_main_run_string_end(minst, 1, &xec, &xeo)
  573.         start_x();
  574.         run_x("\216\003abc");
  575.         run_x("== flush\n");
  576.         stop_x();
  577.         }
  578.         gs_exit(0);
  579. /*#endif */
  580.     case 'Z':
  581.         {
  582.         byte value = (*arg == '-' ? (++arg, 0) : 0xff);
  583.  
  584.         while (*arg)
  585.             gs_debug[*arg++ & 127] = value;
  586.         }
  587.         break;
  588.     }
  589.     return 0;
  590. }
  591.  
  592. /* Define versions of strlen and strcat that encode strings in hex. */
  593. /* This is so we can enter escaped characters regardless of whether */
  594. /* the Level 1 convention of ignoring \s in strings-within-strings */
  595. /* is being observed (sigh). */
  596. private int
  597. esc_strlen(const char *str)
  598. {
  599.     return strlen(str) * 2 + 2;
  600. }
  601. private void
  602. esc_strcat(char *dest, const char *src)
  603. {
  604.     char *d = dest + strlen(dest);
  605.     const char *p;
  606.     static const char *const hex = "0123456789abcdef";
  607.  
  608.     *d++ = '<';
  609.     for (p = src; *p; p++) {
  610.     byte c = (byte) * p;
  611.  
  612.     *d++ = hex[c >> 4];
  613.     *d++ = hex[c & 0xf];
  614.     }
  615.     *d++ = '>';
  616.     *d = 0;
  617. }
  618.  
  619. /* Process file names */
  620. private int
  621. argproc(gs_main_instance * minst, const char *arg)
  622. {
  623.     if (minst->run_buffer_size) {
  624.     /* Run file with run_string. */
  625.     return run_buffered(minst, arg);
  626.     } else {
  627.     /* Run file directly in the normal way. */
  628.     return runarg(minst, "", arg, ".runfile", runInit | runFlush);
  629.     }
  630. }
  631. private int
  632. run_buffered(gs_main_instance * minst, const char *arg)
  633. {
  634.     FILE *in = gp_fopen(arg, gp_fmode_rb);
  635.     int exit_code;
  636.     ref error_object;
  637.     int code;
  638.  
  639.     if (in == 0) {
  640.     fprintf(minst->fstdout, "Unable to open %s for reading", arg);
  641.     return_error(e_invalidfileaccess);
  642.     }
  643.     code = gs_main_init2(minst);
  644.     if (code < 0)
  645.     return code;
  646.     code = gs_main_run_string_begin(minst, minst->user_errors,
  647.                     &exit_code, &error_object);
  648.     if (!code) {
  649.     char buf[MAX_BUFFERED_SIZE];
  650.     int count;
  651.  
  652.     code = e_NeedInput;
  653.     while ((count = fread(buf, 1, minst->run_buffer_size, in)) > 0) {
  654.         code = gs_main_run_string_continue(minst, buf, count,
  655.                            minst->user_errors,
  656.                            &exit_code, &error_object);
  657.         if (code != e_NeedInput)
  658.         break;
  659.     }
  660.     if (code == e_NeedInput) {
  661.         code = gs_main_run_string_end(minst, minst->user_errors,
  662.                       &exit_code, &error_object);
  663.     }
  664.     }
  665.     fclose(in);
  666.     zflush(minst->i_ctx_p);
  667.     zflushpage(minst->i_ctx_p);
  668.     return run_finish(minst, code, exit_code, &error_object);
  669. }
  670. private int
  671. runarg(gs_main_instance * minst, const char *pre, const char *arg,
  672.        const char *post, int options)
  673. {
  674.     int len = strlen(pre) + esc_strlen(arg) + strlen(post) + 1;
  675.     char *line;
  676.  
  677.     if (options & runInit) {
  678.     int code = gs_main_init2(minst);    /* Finish initialization */
  679.  
  680.     if (code < 0)
  681.         return code;
  682.     }
  683.     line = (char *)gs_alloc_bytes(minst->heap, len, "argproc");
  684.     if (line == 0) {
  685.     lprintf("Out of memory!\n");
  686.     return_error(e_VMerror);
  687.     }
  688.     strcpy(line, pre);
  689.     esc_strcat(line, arg);
  690.     strcat(line, post);
  691.     return run_string(minst, line, options);
  692. }
  693. private int
  694. run_string(gs_main_instance * minst, const char *str, int options)
  695. {
  696.     int exit_code;
  697.     ref error_object;
  698.     int code = gs_main_run_string(minst, str, minst->user_errors,
  699.                   &exit_code, &error_object);
  700.  
  701.     if ((options & runFlush) || code != 0) {
  702.     zflush(minst->i_ctx_p);        /* flush stdout */
  703.     zflushpage(minst->i_ctx_p);    /* force display update */
  704.     }
  705.     return run_finish(minst, code, exit_code, &error_object);
  706. }
  707. private int
  708. run_finish(gs_main_instance *minst, int code, int exit_code,
  709.        ref * perror_object)
  710. {
  711.     switch (code) {
  712.     case 0:
  713.         break;
  714.     case e_Quit:
  715.         gs_exit(0);
  716.         /* NOTREACHED */
  717.         break;
  718.     case e_Fatal:
  719.         eprintf1("Unrecoverable error, exit code %d\n", exit_code);
  720.         gs_exit(exit_code);
  721.         /* NOTREACHED */
  722.         break;
  723.     default:
  724.         gs_main_dump_stack(minst, code, perror_object);
  725.         gs_exit_with_code(255, code);
  726.         /* NOTREACHED */
  727.     }
  728.     return code;
  729. }
  730.  
  731. /* ---------------- Print information ---------------- */
  732.  
  733. /*
  734.  * Help strings.  We have to break them up into parts, because
  735.  * the Watcom compiler has a limit of 510 characters for a single token.
  736.  * For PC displays, we want to limit the strings to 24 lines.
  737.  */
  738. private const char help_usage1[] = "\
  739. Usage: gs [switches] [file1.ps file2.ps ...]\n\
  740. Most frequently used switches: (you can use # in place of =)\n\
  741.  -dNOPAUSE           no pause after page   | -q       `quiet', fewer messages\n\
  742.  -g<width>x<height>  page size in pixels   | -r<res>  pixels/inch resolution\n";
  743. private const char help_usage2[] = "\
  744.  -sDEVICE=<devname>  select device         | -dBATCH  exit after last file\n\
  745.  -sOutputFile=<file> select output file: - for stdout, |command for pipe,\n\
  746.                                          embed %d or %ld for page #\n";
  747. private const char help_trailer[] = "\
  748. For more information, see %s%sUse.htm.\n\
  749. Report bugs to %s, using the form in Bug-form.htm.\n";
  750. private const char help_devices[] = "Available devices:";
  751. private const char help_emulators[] = "Input formats:";
  752. private const char help_paths[] = "Search path:";
  753.  
  754. /* Print the standard help message. */
  755. private void
  756. print_help(gs_main_instance * minst)
  757. {
  758.     print_revision(minst);
  759.     print_usage(minst);
  760.     print_emulators(minst);
  761.     print_devices(minst);
  762.     print_paths(minst);
  763.     if (gs_init_string_sizeof > 0) {
  764.         fprintf(minst->fstdout,
  765.         "Initialization files are compiled into the executable.\n");
  766.     }
  767.     print_help_trailer(minst);
  768. }
  769.  
  770. /* Print the revision, revision date, and copyright. */
  771. private void
  772. print_revision(const gs_main_instance *minst)
  773. {
  774.     FILE *out = minst->fstdout;
  775.  
  776.     printf_program_ident(out, gs_product, gs_revision);
  777.     fprintf(out, " (%d-%02d-%02d)\n%s\n",
  778.         (int)(gs_revisiondate / 10000),
  779.         (int)(gs_revisiondate / 100 % 100),
  780.         (int)(gs_revisiondate % 100),
  781.         gs_copyright);
  782. }
  783.  
  784. /* Print the version number. */
  785. private void
  786. print_version(const gs_main_instance *minst)
  787. {
  788.     FILE *out = minst->fstdout;
  789.  
  790.     printf_program_ident(out, NULL, gs_revision);
  791. }
  792.  
  793. /* Print usage information. */
  794. private void
  795. print_usage(const gs_main_instance *minst)
  796. {
  797.     FILE *out = minst->fstdout;
  798.  
  799.     fprintf(out, "%s", help_usage1);
  800.     fprintf(out, "%s", help_usage2);
  801. }
  802.  
  803. /* Print the list of available devices. */
  804. private void
  805. print_devices(const gs_main_instance *minst)
  806. {
  807.     FILE *out = minst->fstdout;
  808.  
  809.     fprintf(out, "%s", help_devices);
  810.     {
  811.     int i;
  812.     int pos = 100;
  813.     const gx_device *pdev;
  814.  
  815.     for (i = 0; (pdev = gs_getdevice(i)) != 0; i++) {
  816.         const char *dname = gs_devicename(pdev);
  817.         int len = strlen(dname);
  818.  
  819.         if (pos + 1 + len > 76)
  820.         fprintf(out, "\n  "), pos = 2;
  821.         fprintf(out, " %s", dname);
  822.         pos += 1 + len;
  823.     }
  824.     }
  825.     fprintf(out, "\n");
  826. }
  827.  
  828. /* Print the list of language emulators. */
  829. private void
  830. print_emulators(const gs_main_instance *minst)
  831. {
  832.     FILE *out = minst->fstdout;
  833.  
  834.     fprintf(out, "%s", help_emulators);
  835.     {
  836.     const ref *pes;
  837.  
  838.     for (pes = gs_emulator_name_array;
  839.          pes->value.const_bytes != 0; pes++
  840.         )
  841.         /*
  842.          * Even though gs_emulator_name_array is declared and used as
  843.          * an array of string refs, each string is actually a
  844.          * (null terminated) C string.
  845.          */
  846.         fprintf(out, " %s", (const char *)pes->value.const_bytes);
  847.     }
  848.     fprintf(out, "\n");
  849. }
  850.  
  851. /* Print the search paths. */
  852. private void
  853. print_paths(gs_main_instance * minst)
  854. {
  855.     FILE *out = minst->fstdout;
  856.  
  857.     fprintf(out, "%s", help_paths);
  858.     gs_main_set_lib_paths(minst);
  859.     {
  860.     uint count = r_size(&minst->lib_path.list);
  861.     uint i;
  862.     int pos = 100;
  863.     char fsepr[3];
  864.  
  865.     fsepr[0] = ' ', fsepr[1] = gp_file_name_list_separator,
  866.         fsepr[2] = 0;
  867.     for (i = 0; i < count; ++i) {
  868.         const ref *prdir =
  869.         minst->lib_path.list.value.refs + i;
  870.         uint len = r_size(prdir);
  871.         const char *sepr = (i == count - 1 ? "" : fsepr);
  872.  
  873.         if (1 + pos + strlen(sepr) + len > 76)
  874.         fprintf(out, "\n  "), pos = 2;
  875.         fprintf(out, " ");
  876.         /*
  877.          * This is really ugly, but it's necessary because some
  878.          * platforms rely on all console output being funneled through
  879.          * fprintf.  We wish we could just do:
  880.          fwrite(prdir->value.bytes, 1, len, out);
  881.          */
  882.         {
  883.         const char *p = (const char *)prdir->value.bytes;
  884.         uint j;
  885.  
  886.         for (j = len; j; j--)
  887.             fprintf(out, "%c", *p++);
  888.         }
  889.         fprintf(out, sepr);
  890.         pos += 1 + len + strlen(sepr);
  891.     }
  892.     }
  893.     fprintf(out, "\n");
  894. }
  895.  
  896. /* Print the help trailer. */
  897. private void
  898. print_help_trailer(const gs_main_instance *minst)
  899. {
  900.     FILE *out = minst->fstdout;
  901.  
  902.     fprintf(out, help_trailer, gs_doc_directory,
  903.         gp_file_name_concat_string(gs_doc_directory,
  904.                        strlen(gs_doc_directory),
  905.                        "Use.htm", 7),
  906.         GS_BUG_MAILBOX);
  907. }
  908.